package com.anfeng.cms.service.impl.emp;

import com.anfeng.cms.common.Common;
import com.anfeng.cms.domain.emp.AfGuestLog;
import com.anfeng.cms.repository.mysql.dao.emp.AfGuestLogMapper;
import com.anfeng.cms.request.emp.GuestLogReq;
import com.anfeng.cms.service.emp.IAfGuestLogService;
import com.anfeng.cms.util.IdGeneratorUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴朋
 * @since 2019-07-18
 */
@Service
@Slf4j
public class AfGuestLogServiceImpl extends ServiceImpl<AfGuestLogMapper, AfGuestLog> implements IAfGuestLogService {

    /**
     * 新增访客记录流水
     *
     * @param guestLog 访客记录
     * @return 访客访问流水id
     */
    @Override
    public String addGuestLog(AfGuestLog guestLog) {
        if (null == guestLog || StringUtils.isBlank(guestLog.getEmpId()) || StringUtils.isBlank(guestLog.getEmpMobile())
                || StringUtils.isBlank(guestLog.getEmpName()) || StringUtils.isBlank(guestLog.getGuestId())) {
            log.error("新增访客流水失败 传入参数为null");
            return null;
        }
        String logId = IdGeneratorUtil.uuid();
        guestLog.setId(logId);
        guestLog.setState(Common.DATA_STATE_NORMAL);
        Date currentDate = new Date();
        guestLog.setCreateTime(currentDate);
        guestLog.setModifiedTime(currentDate);
        boolean result = this.insert(guestLog);
        if (!result) {
            log.error("新增访客流水失败 访客信息{}", guestLog);
            return null;
        }
        return logId;
    }

    /**
     * 根据查询条件获取 访客流水条数
     *
     * @param guestLogReq 查询条件
     * @return 访问流水条数
     */
    @Override
    public int getGuestLogCounts(GuestLogReq guestLogReq) {
        EntityWrapper<AfGuestLog> wrapper = this.generatorGuestLogWrapper(guestLogReq);
        return this.selectCount(wrapper);
    }

    /**
     * 根据查询条件获取 访客流水
     *
     * @param guestLogReq 查询条件
     * @return 访问流水
     */
    @Override
    public List<AfGuestLog> getAllGuestLogs(GuestLogReq guestLogReq) {
        EntityWrapper<AfGuestLog> wrapper = this.generatorGuestLogWrapper(guestLogReq);
        return this.selectList(wrapper);
    }

    /**
     * 根据查询条件 分页获取 访客流水
     *
     * @param guestLogReq 查询条件
     * @param page        当前页
     * @param pageSize    每页显示数量
     * @return 访问流水信息
     */
    @Override
    public List<AfGuestLog> getGuestLogsByPage(GuestLogReq guestLogReq, Integer page, Integer pageSize) {
        if (null == page || page < 1) {
            page = 1;
        }
        if (null == pageSize || pageSize < 10) {
            pageSize = 10;
        }
        Page<AfGuestLog> guestLogPage = new Page<>();
        guestLogPage.setSize(pageSize);
        guestLogPage.setCurrent(page);
        EntityWrapper<AfGuestLog> wrapper = this.generatorGuestLogWrapper(guestLogReq);
        Page<AfGuestLog> afGuestLogPage = this.selectPage(guestLogPage, wrapper);
        if (null == afGuestLogPage || null == afGuestLogPage.getRecords() || afGuestLogPage.getRecords().isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        return afGuestLogPage.getRecords();
    }

    /**
     * 根据查询条件生成 查询wrapper
     * @param guestLogReq 查询条件
     * @return wrapper
     */
    private EntityWrapper<AfGuestLog> generatorGuestLogWrapper(GuestLogReq guestLogReq) {
        if (null == guestLogReq) {
            guestLogReq = new GuestLogReq();
        }
        AfGuestLog guestLog = new AfGuestLog();
        guestLog.setGuestId(guestLogReq.getGuestId());
        guestLog.setEmpMobile(guestLogReq.getEmpMobile());

        EntityWrapper<AfGuestLog> wrapper = new EntityWrapper<>();
        wrapper.setEntity(guestLog);
        wrapper.orderBy("create_time", false);
        return wrapper;
    }
}
